package com.base_data_handler.sx.handler;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.httpclient.util.DateUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base_data_handler.sx.utils.WarningLevel;
import com.base_data_handler.utils.DataSourceUtils;
import com.base_data_handler.utils.DruidPlugin;
import com.base_data_handler.utils.HttpUtils;
import com.base_data_handler.utils.StringUtils;
import com.mysql.fabric.xmlrpc.base.Data;
/**
 * cimmiss  陕西省预警信息 3分钟采集一次
 * @param urlStr
 * @return
 */
public class SX_Warning_Handler  {
//	private	static QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	private	static QueryRunner qr=new QueryRunner(DataSourceUtils.getDatasSource());

	private static final Logger log = Logger.getLogger(SX_Warning_Handler.class.getName());
	
	
	public	static boolean getCimmisSX_Warning() throws SQLException{
		String times="",times2="";//20170627000000
		Calendar c=Calendar.getInstance();
		int year=c.get(Calendar.YEAR);
		String month=(c.get(Calendar.MONTH)+1)+"";
		String day=c.get(Calendar.DAY_OF_MONTH)+"";
		String hour=c.get(Calendar.HOUR_OF_DAY)+"";
		
		if(month.length()==1)month="0"+month;
		if(day.length()==1)day="0"+day;
		
//		System.out.println("year="+year+",month="+month+",day="+day);
		times=year+month+day+"000000";
		times2=year+month+day+"235959";
		String url="http://10.172.89.55/cimiss-web/api";
		
		String param="userId=BEXA_BAOJI_YW&pwd=baoji123&interfaceId=getSevpEleByTimeRange&dataCode=SEVP_BEXA_WARN_INFO"
				+ "&elements=DATA_ID,Datetime,SIGNALTYPE,SIGNALLEVEL,ISSUECONTENT,MSGTYPE,UNDERWRITER,REFERENCE,EXPIRES,SENDER,INVALID,CREATEDBY,CREATED,UPDATEDBY,UPDATED,AREA_ID,TYPE,CITYLON,CITYLAT,CODEID,CODECODING,CODETYPE,FATHERID,FATHERNAME,CODESTATUS,WARN_SIG_KEY"
				+ "&timeRange=["+times+","+times2+"]&dataFormat=json";
		
		Map<String, String> map=new HashMap<String, String>();
		String[] key_value = param.split("&");
		for (int i = 0; i < key_value.length; i++) {
			String[] k_v = key_value[i].split("=");
			map.put(k_v[0], k_v[1]);
		 }
		 String str = HttpUtils.sendHttpClientPost(url, map, "utf-8");
		 
		 JSONObject alljson= (JSONObject) JSON.parse(str);
		 if(alljson.getString("returnCode")!=null&&alljson.getString("returnCode").equals("0")){
			  JSONArray mod = JSON.parseArray(alljson.getString("DS"));
				Object[][] params=new Object[mod.size()][35];
			   JSONObject obj=null;
			   for (int i = 0; i < mod.size(); i++) {
				    obj = (JSONObject) mod.get(i);
//				    System.out.println(mod.get(i));
				    params[i][0]=obj.getString("DATA_ID");
				    params[i][1]=obj.getString("Datetime");
				    params[i][2]=obj.getString("SIGNALTYPE");
				    params[i][3]=obj.getString("SIGNALLEVEL");
				    params[i][4]=obj.getString("ISSUECONTENT");
				    params[i][5]=obj.getString("MSGTYPE");
				    params[i][6]=obj.getString("UNDERWRITER");
				    params[i][7]=obj.getString("REFERENCE");
				    params[i][8]=obj.getString("EXPIRES");
				    params[i][9]=obj.getString("SENDER");
				    params[i][10]=obj.getString("INVALID");
				    params[i][11]=obj.getString("CREATEDBY");
				    params[i][12]=StringUtils.isNotEmpty(obj.getString("CREATED"))?obj.getString("CREATED"):DateUtil.formatDate(new Date(), "yyyy/MM/dd HH:mm:ss");
				    params[i][13]=obj.getString("UPDATEDBY");
				    params[i][14]=StringUtils.isNotEmpty(obj.getString("UPDATED"))?obj.getString("CREATED"):DateUtil.formatDate(new Date(), "yyyy/MM/dd HH:mm:ss");
				    params[i][15]=obj.getString("AREA_ID");
				    params[i][16]=obj.getString("TYPE");
				    params[i][17]=obj.getString("CITYLON");
				    params[i][18]=obj.getString("CITYLAT");
				    params[i][19]=obj.getString("CODEID");
				    params[i][20]=obj.getString("CODECODING");
				    params[i][21]=obj.getString("CODETYPE");
				    params[i][22]=obj.getString("FATHERID");
				    params[i][23]=obj.getString("FATHERNAME");
				    params[i][24]=obj.getString("CODESTATUS");
				    params[i][25]=obj.getString("WARN_SIG_KEY");
				    //处理过期时间
				    int hours= WarningLevel.getWarnOutTime(obj.getString("SIGNALTYPE"), obj.getString("SIGNALLEVEL"));
				    params[i][26]= WarningLevel.dateAddHours(hours,obj.getString("Datetime").replace("-", "/"));
				    params[i][27]=obj.getString("SIGNALTYPE");
				    params[i][28]=obj.getString("SIGNALLEVEL");
				    params[i][29]=obj.getString("ISSUECONTENT");
				    params[i][30]=obj.getString("UNDERWRITER");
				    params[i][31]=obj.getString("SENDER");
				    params[i][32]=obj.getString("INVALID");
				    params[i][33]=obj.getString("CODETYPE");
				    params[i][34]=obj.getString("CODESTATUS");
				    
				   
			    }
			   
			    proData2Db(params);
		 }
		 return false;
	}
	
	private static void proData2Db(Object[][] params) throws SQLException{
		StringBuilder sql=new StringBuilder();
		

		sql.append("INSERT INTO   cmiss_warning ")
		   .append("( DATA_ID,Datetime,SIGNALTYPE,SIGNALLEVEL,ISSUECONTENT,MSGTYPE,UNDERWRITER,REFERENCE,EXPIRES,SENDER,INVALID,CREATEDBY,CREATED,UPDATEDBY,UPDATED,AREA_ID,TYPE,CITYLON,CITYLAT,CODEID,CODECODING,CODETYPE,FATHERID,FATHERNAME,CODESTATUS,WARN_SIG_KEY,OUT_DATA) ")
		   .append(" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")			
		   .append(" ON DUPLICATE KEY UPDATE  ")
		   .append(" SIGNALTYPE=?,SIGNALLEVEL=?,ISSUECONTENT=?,UNDERWRITER=?,")
		   .append(" SENDER =?,INVALID=?,CODETYPE=?,CODESTATUS=?");//批量
		qr.batch(sql.toString(), params);
	}
	public static void main(String[] args) throws SQLException   {
			getCimmisSX_Warning();
	}
	
}
